package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.entity.SpuImage;
import com.atguigu.gmall.model.product.entity.SpuSaleAttr;
import com.atguigu.gmall.model.product.entity.SpuSaleAttrValue;
import com.atguigu.gmall.model.product.vo.SpuInfoVO;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.service.SpuSaleAttrValueService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.model.product.entity.SpuInfo;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* @author jiamin
* @description 针对表【spu_info(商品表)】的数据库操作Service实现
* @createDate 2023-08-14 20:41:58
*/
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
    implements SpuInfoService{

    @Autowired
    SpuImageService spuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    @Autowired
    SpuSaleAttrValueService spuSaleAttrValueService;

    @Transactional
    @Override
    public void saveSpuInfo(SpuInfoVO spuInfoVO) {
        //1. 先保存spu_info 信息生成id供后续使用
        SpuInfo spuInfo = new SpuInfo();
        BeanUtils.copyProperties(spuInfoVO, spuInfo);
        this.save(spuInfo);
        //2. 获取spuId
        Long spuId = spuInfo.getId();

        //3. 插入spu_img数据
        List<SpuImage> spuImgList = spuInfoVO.getSpuImageList().stream()
                .map(item -> {
                    SpuImage spuImage = new SpuImage();
                    BeanUtils.copyProperties(item, spuImage);
                    spuImage.setSpuId(spuId);
                    return spuImage;
                }).collect(Collectors.toList());
        //批量插入spu_img数据
        spuImageService.saveBatch(spuImgList);

        //4. 插入销售属性名称
        List<SpuSaleAttr> spuSaleAttrList = spuInfoVO.getSpuSaleAttrList().stream()
                .map(item -> {
                    SpuSaleAttr spuSaleAttr = new SpuSaleAttr();
                    BeanUtils.copyProperties(item, spuSaleAttr);
                    spuSaleAttr.setSpuId(spuId);
                    return spuSaleAttr;
                }).collect(Collectors.toList());
        spuSaleAttrService.saveBatch(spuSaleAttrList);

        //5.插入销售属性值表，flatMap是一个用于处理嵌套流的操作。它将原始流中的每个元素映射为一个新的流，
        // 并将所有新的流扁平化为单个流。具体来说，flatMap需要一个函数作为参数，该函数将原始流中的每个元素映射为一个流。
        List<SpuSaleAttrValue> spuSaleAttrValueList = spuInfoVO.getSpuSaleAttrList().stream()
                .flatMap(item -> item.getSpuSaleAttrValueList().stream()
                        .map(value -> {
                            value.setSaleAttrName(item.getSaleAttrName());
                            return value;
                        })
                ).map(item -> {
                    SpuSaleAttrValue spuSaleAttrValue = new SpuSaleAttrValue();
                    BeanUtils.copyProperties(item, spuSaleAttrValue);
                    spuSaleAttrValue.setSpuId(spuId);
                    spuSaleAttrValue.setSaleAttrName(item.getSaleAttrName());
                    return spuSaleAttrValue;
                }).collect(Collectors.toList());

        spuSaleAttrValueService.saveBatch(spuSaleAttrValueList);


    }
}




